import Vue from 'vue';
import {
    url,
    isFunction,
} from '@/lib/utils';

import {
    api,
} from '@/lib/api';
import store from '@/store/index';
import {
    STATUS_LOGIN_LOGINED,
    STATUS_LOGIN_LOGOUT,
} from '@/lib/constant';

import { Module } from 'vuex';
import { RootState } from '@/types/store';

export interface UtilsState {
    // 其他用户信息
    userInfo: User.BaseInfo | null;
    // 系统信息
    systemInfo: any;
    menuButtonClientRect: any;
    // 系统类型
    system: any;
    // 进入验证页面
    inAuthPage: any;
    // 页面顶部位置
    pageTop: any;
    // 导航深度
    navDepth: any;
    /** 是否是preview吊起的onhide */
    isPreviewHide: any;
    /** 客服信息 */
    service: Framework.Service | null;
    // 返利规则图片
    rebateRules: any;
    /** 请求数量 */
    req: number;
    /** 下拉刷新数量 */
    pulldown: number;
    /** 场景内容 */
    scene: any;

    imagePopup: {
        visible: boolean;
        src?: string;
        fn?: Function;
    };
}

const m: Module<UtilsState, RootState> = {
    namespaced: true,
    state: {
        // 其他用户信息
        userInfo: {},
        // 系统信息
        systemInfo: null,
        menuButtonClientRect: {
            top: 26,
            height: 32,
        },
        // 系统类型
        system: null,
        // 进入验证页面
        inAuthPage: false,
        // 页面顶部位置
        pageTop: 0,
        // 导航深度
        navDepth: 1,
        /** 是否是preview吊起的onhide */
        isPreviewHide: false,
        /** 客服信息 */
        service: null,
        // 返利规则图片
        rebateRules: [],
        req: 0,
        pulldown: 0,
        scene: null,
        imagePopup: {
            visible: false,
            src: '',
            fn: undefined,
        },
    },
    mutations: {
        /**
         * 设置其他用户基本信息
         */
        setUserInfo (state, { id, data }) {
            Vue.set(state.userInfo, id, data);
        },
        /**
         * 获取系统信息
         * @param {} systemInfo 系统信息
         */
        setSystemInfo(state, systemInfo) {
            state.systemInfo = systemInfo;
            const system = state.systemInfo.system;
            if (system) {
                if (system.indexOf('iOS') >= 0) {
                    state.system = 'ios';
                } else {
                    state.system = 'android';
                }
            }
        },
        /**
         * 获取头部按钮信息
         */
        setMenuButtonClientRect (state, menuButtonClientRect) {
            state.menuButtonClientRect = menuButtonClientRect;
        },
        /**
         * 标记是否进入到auth page
         * @param {} in
         */
        setInAuthPage(state, inAuthPage) {
            state.inAuthPage = inAuthPage;
        },
        /**
         * 记录顶部位置
         */
        setPageTop(state, top) {
            state.pageTop = top;
        },
        /**
         * 记录导航深度
         */
        setNavDepth(state, depth) {
            state.navDepth = depth;
        },
        /** 设置onhide变量 */
        setIsPreviewHide(state, bol) {
            state.isPreviewHide = bol;
        },
        /**
         * 记录客服信息
         */
        setService(state, service) {
            state.service = service;
        },
        // 记录返利规则
        setRebateRules(state, rules) {
            state.rebateRules = rules;
        },
        startRequest(state) {
            state.req++;
        },
        endRequest(state) {
            if (state.req <= 0) {
                return;
            }
            state.req--;
        },
        startPulldown(state) {
            state.pulldown++;
        },
        endPulldown(state) {
            if (state.pulldown <= 0) {
                return;
            }
            state.pulldown--;
        },
        setScene(state, scene) {
            state.scene = scene;
        },
        setImagePopup(state, imagePopup) {
            state.imagePopup = imagePopup;
        },
    },
    actions: {
        /**
         * 上传从微信处获取的用户信息
         *  @param {} encryptedData
         *  @param {} iv
         */
        async syncWechat({ state, commit }, { encryptedData, iv }) {
            const res = await api.post({
                url: `${url}/api/user/sync_user_info`,
                data: {
                    encrypted_data: encryptedData,
                    iv: iv,
                },
            });
            return res;
        },
        /**
         * 解密数据
         * @param {} encryptedData
         * @param {} iv
         */
        async decryptData (context, { encryptedData, iv }) {
            const res = await api.post({
                url: `${url}/api/encryptor`,
                data: {
                    encrypted_data: encryptedData,
                    iv: iv,
                },
            });
            return res;
        },
        /**
         * 获取页面配置
         */
        async getPageConfig({ state, commit }, { forceUpdate } = {}) {
            if (state.service && !forceUpdate) {
                return;
            }
            const res = await api.get({
                url: `${url}/api/page`,
            });
            if (res.statusCode === 200 && res.data.status === 0) {
                commit('setService', res.data.data);
            }
            return res;
        },
        async getRebateRules({ state, commit, }, { forceUpdate, } = {}) {
            if (state.rebateRules.length && !forceUpdate) {
                return;
            }
            const res = await api.get({
                url: `${url}/api/rebate_rule`,
            });
            if (res.statusCode === 200 && res.data.status === 0 && res.data.data) {
                commit('setRebateRules', res.data.data);
            }
            return res;
        },
    },
};

export default m;
